using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace kattspel
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteBatch spriteBatch2;
        Wizard mWizardSprite;
        //Create a Horizontally scrolling background
        HorizontallyScrollingBackground mScrollingBackground;
        Kloss movingkloss;
        Texture2D bkloss;
        Texture2D bkloss1;
        Texture2D bkloss2;
        Random rand = new Random();

        double poang;
        double min;
        Vector2 spritePosition = Vector2.Zero;
        Vector2 spritePosition1 = Vector2.Zero;
        Vector2 spritePosition2 = Vector2.Zero;


        SpriteFont ArialFont;

        Song mysong;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";


            

            mysong = Content.Load<Song>("Kalimba");
            MediaPlayer.Play(mysong);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            spritePosition = new Vector2(rand.Next(150, 500), rand.Next(150, 500));
            spritePosition2 = new Vector2(rand.Next(150, 400), rand.Next(150, 400));
            spritePosition1 = new Vector2(rand.Next(150, 300), rand.Next(150, 300));
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            spriteBatch2 = new SpriteBatch(GraphicsDevice);
            mWizardSprite = new Wizard();
            movingkloss = new Kloss();
            bkloss = Content.Load<Texture2D>("kloss");
            bkloss1 = Content.Load<Texture2D>("kloss");
            bkloss2 = Content.Load<Texture2D>("kloss");

           

            //spritePosition = new Vector2(rand.Next(10, 600), rand.Next(10, 600));
            //spritePosition = new Vector2(rand.Next(10, 600), rand.Next(10, 600)); 


            mWizardSprite.LoadContent(this.Content);
            //Initialize and add the background images to the Scrolling background. You can change the
            //scroll area by passing in a different Viewport. The images will then scale and scroll within
            //that given Viewport.
            mScrollingBackground = new HorizontallyScrollingBackground(this.GraphicsDevice.Viewport);
            mScrollingBackground.AddBackground("Background01");
            mScrollingBackground.AddBackground("Background02");
            mScrollingBackground.AddBackground("Background03");
            mScrollingBackground.AddBackground("Background04");
            mScrollingBackground.AddBackground("Background05");
            

            ArialFont = Content.Load<SpriteFont>("SpriteFont1");
            //Load the content for the Scrolling background
            mScrollingBackground.LoadContent(this.Content);
            movingkloss.LoadContent(this.Content);
 


           // movingkloss.klossPosition = new Vector2(rand.Next(2, 11), rand.Next(2, 11));

            //movingkloss.klossPosition= new Vector2(rand.Next(2, 11), rand.Next(2, 11)); 


        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        double startTime = 0;
        int v = 160;
        protected override void Update(GameTime gameTime)
        {
            startTime += gameTime.ElapsedGameTime.TotalSeconds;
            mScrollingBackground.Update(gameTime, v, HorizontallyScrollingBackground.HorizontalScrollDirection.Left);
            if (startTime >= 5)
            {
                mScrollingBackground.Update(gameTime, v += 100, HorizontallyScrollingBackground.HorizontalScrollDirection.Left);
                startTime -= 5;
            
            }

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            mWizardSprite.Update(gameTime);
            
            //Update the scrolling backround. You can scroll to the left or to the right by changing the scroll direction
            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            //spritePosition = new Vector2(rand.Next(10, 600), rand.Next(10, 600));
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            mScrollingBackground.Draw(spriteBatch);
            mWizardSprite.Draw(spriteBatch);
            movingkloss.Draw(spriteBatch);

            spriteBatch.Draw(bkloss, spritePosition, Color.White);
            spriteBatch.Draw(bkloss1, spritePosition1, Color.White);
            spriteBatch.Draw(bkloss2, spritePosition2, Color.White);
   

            score();

            spriteBatch.End();
            /*spriteBatch2.Begin();
            //Draw the scrolling background
            mWizardSprite.Draw(spriteBatch2);
            mScrollingBackground.Draw(spriteBatch);
            spriteBatch2.End();*/

            base.Draw(gameTime);
        }
        private void score()
        {
            
            min = min + 1;
            spriteBatch.DrawString(ArialFont, "Time: " + min.ToString(), new Vector2(20, 20), Color.White);

            poang = poang + 0.5;
            spriteBatch.DrawString(ArialFont, "Score:" + poang.ToString(), new Vector2(600, 20), Color.White);

        }

    }
}
